package com.hj.security.ui;

import com.hj.security.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SetupGuide2Activity extends Activity implements OnClickListener {

	private SharedPreferences sp;
	private Button bt_bind;
	private Button bt_next;
	private Button bt_previous;
	private CheckBox cb_bind;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_guide2);

		sp = getSharedPreferences("config", this.MODE_PRIVATE);

		bt_bind = (Button) findViewById(R.id.bt_guide_bind);
		bt_next = (Button) findViewById(R.id.bt_guide_next);
		bt_previous = (Button) findViewById(R.id.bt_guide_previous);
		cb_bind = (CheckBox) findViewById(R.id.cb_guide_check);

		bt_bind.setOnClickListener(this);
		bt_next.setOnClickListener(this);
		bt_previous.setOnClickListener(this);
		
		//初始化CheckBox状态
		String sim = sp.getString("simSerial", null);
		if(sim!=null){
			cb_bind.setText("已经绑定");
			cb_bind.setChecked(true);
		}else{
			cb_bind.setText("没有绑定");
			cb_bind.setChecked(false);
			resetSimInfo();
		}
		cb_bind.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					cb_bind.setText("已经绑定");
					setSimInfo();
				}else{
					cb_bind.setText("没有绑定");
					resetSimInfo();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_guide_bind:
			setSimInfo();
			cb_bind.setText("已经绑定");
			cb_bind.setChecked(true);
			break;
		case R.id.bt_guide_next:
			Intent intent = new Intent(this, SetupGuide3Activity.class);
			finish();
			startActivity(intent);
			overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);  
			break;
		case R.id.bt_guide_previous:
			Intent i = new Intent(this, SetupGuide1Activity.class);
			finish();
			startActivity(i);
			overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);  
			break;

		default:
			break;
		}

	}
	
	private void setSimInfo(){
		TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		String simSerial = telephonyManager.getSimSerialNumber();
		
		Editor editor = sp.edit();
		editor.putString("simSerial", simSerial);
		editor.commit();
	}
	private void resetSimInfo() {
		Editor editor = sp.edit();
		editor.putString("simSerial", null);
		editor.commit();
	}	
}
